<?php
namespace app\index\controller;

use think\Request;
use \app\admin\model\Build as BuildModel;

class Build extends Base
{
    protected $model;
    
    public function __construct(Request $request = null)
    {
        parent::__construct($request);

        $this->model = new BuildModel();
    }

    public function index($id)
    {
        // 正文
        $data = $this->model->find($id);

        $data->clicks +=1;
        
        @$data->save();

        // banner图
        $banner = $this->category_model->where('id', $data->cid)->value('banner');

        $data->banner = $banner;

        // 产品图册
        $pictures = new \app\admin\model\Pictures();

        $data_pic = $pictures->field('id,cid,thumb,image')
            ->where(['cid'=>$data->cid, 'sid'=>$id, 'status'=>1])
            ->select();

        $this->assign('data', $data);
        $this->assign('data_pic', $data_pic);
        return $this->fetch();
    }

    public function lists($cid = 51)
    {
        // 后面就变了,先输出当前cid
        $this->assign('cid', $cid);

        // banner,img_title图
        $data_banner =  $this->category_model->where('id', $cid)->field('banner, img_title')->find();
        
        if ($cid == 51) {
            $cid = $this->category_model->where('pid', $cid)->column('id');
        }
        $data = $this->model->field('id,title,thumb,summary')
            ->where('cid', 'in', $cid)
            ->where('status', 1)
            ->order('top')
            ->paginate(6);

        // return json_encode($data);
        $this->assign('data', $data);
        $this->assign('data_banner', $data_banner);
        return $this->fetch();
    }
}
